/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebServer;

import connezione.BeanPool;
import connezione.ConnectionPool;
import connezione.PooledConnection;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

public class CustomerResourceService implements CustomerResource {

    private ConnectionPool pool;

    public CustomerResourceService() {

        BeanPool bp = new BeanPool();

        pool = bp.getPool();
    }

    public PrintStream checkSms(String numero, String comandosms, OutputStream os, PooledConnection con) {

        PrintStream response = new PrintStream(os);
        try {
            if (comandosms.equalsIgnoreCase("V")) {

                String comand = "SELECT data_scadenza FROM Visita WHERE id_paziente ='" + numero + "'";
                Statement st = (Statement) con.createStatement();
                ResultSet rs = (ResultSet) st.executeQuery(comand);
                response.println("<?xml version='1.0' encoding='UTF-8'?> ");
                response.println("<Visite>");
                response.println(" <IdPaziente>" + numero + "</IdPaziente>");
                while (rs.next()) {
                    response.println(" <DataVisita>" + rs.getDate("data_scadenza").toString() + "</DataVisita>");
                }
                response.println("</Visite>");
                

            } else {
                response.println("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerResourceService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return response;
    }

    public boolean numeroexsist(String numero, PooledConnection con) {
        String primocomand = "SELECT 1 FROM Anagrafica WHERE cellulare ='" + numero + "'";
        Statement primost;
        try {
            primost = (Statement) con.createStatement();

            ResultSet primors = (ResultSet) primost.executeQuery(primocomand);


            if (primors.next()) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerResourceService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    @Override
    public StreamingOutput getCustomer(final String numero, final String segnalo) {
        final PooledConnection con;
        con = pool.checkOut();


        if (!numeroexsist(numero, con)) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        return new StreamingOutput() {

            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
                checkSms(numero, segnalo, outputStream, con);
                pool.checkIn(con);
            }
        };
    }
}